// pages/games/goldenEggs/index.js
const {
  GamesModel
} = require('../../../utils/games-model.js');
const gameModel = new GamesModel()
const {
  MemberModel
} = require('../../../utils/member-model.js');
const memberModel = new MemberModel();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paursed: false,
    hammer1: false,
    suspend1: false,
    hammer2: false,
    suspend2: false,
    hammer3: false,
    disable1: true,
    authShow: false,
    giftImg: 'https://oss.kiscloud.net/wechat/images/games/gift.png',
    jfImg: 'https://oss.kiscloud.net/wechat/images/games/integral.png',
    themeUrL: 'https://oss.kiscloud.net/wechat/images/share3.jpg',
    disable2: true,
    innerText: '',
    isShow2: true,
    disable3: true,
    suspend3: false,
    imgUrl: 'https://oss.kiscloud.net/wechat/images/winning_ticket.png',
    isShow: true,
    disCountImg: 'https://oss.kiscloud.net/wechat/images/discount_coupon.png',
    memberId: '',
    detailData: {}, // 详情数据
    gift: {
      // activityGiftName: "wj测试折扣券",
      // giftName: "wj测试折扣券",
      // giftType: 3,
    }, // 奖品信息
    gameConfigId: '', // 活动id
    gameConfigName: '', // 游戏名，
    activityType: '', // 游戏类型
    activityId: '', // 活动id
  },
  openEgg1: function (e) {
    const that = this;
    // console.log(11)
    if (that.data.disable1) {
      if (app.globalData.isLogin) {
        const merchantId = wx.getStorageSync('merchantId')
        memberModel.userInfo({
          merchantId: merchantId
        }).then(res => {
          that.setData({
            memberId: res.memberId
          })
          if (that.data.detailData.activityId) {
            that.setData({
              disable2: false,
              disable3: false,
            })
            gameModel.gameLottery({
              "merchantId": merchantId,
              // merchantId: '40ed3fb4-6b8a-4606-9cb3-a1e8bba5584b',
              memberId: that.data.memberId,
              refActivityId: that.data.detailData.activityId
            }).then(data => {
              that.setData({
                paursed: true,
                hammer1: true,
              })
              setTimeout(function () {
                that.setData({
                  suspend1: true,
                  gift: data,
                })
                setTimeout(function () {
                  that.setData({
                    isShow: false
                  })
                  that.revert()
                }, 1000)
              }, 600)
            }).catch(err => {
              // wx.showToast({
              //   title: `${err.error}`,
              //   icon: 'none',
              //   duration: 2000 
              // })
              that.setData({
                isShow2: false,
                innerText: `${err.error ? err.error : '暂无此类活动'}`
              })
              that.revert()
            })
          } else {
            that.setData({
              isShow2: false,
              innerText: `暂无此类活动`
            })
            that.revert()
          }
          // _this.getGameLottery(data.memberId)
        }).catch(err => {
          that.setData({
            isShow2: false,
            innerText: `${err.error ? err.error : '暂无此类活动'}`
          })
        })
      } else {
        this.setData({
          authShow: true
        })
      }
    }
  },
  ok: function () {
    // console.log(111)
  },
  openEgg2: function (e) {
    let that = this;
    if (that.data.disable2) {
      if (app.globalData.isLogin) {
        const merchantId = wx.getStorageSync('merchantId')
        memberModel.userInfo({
          merchantId: merchantId
        }).then(res => {
          that.setData({
            memberId: res.memberId
          })
          if (that.data.detailData.activityId) {
            that.setData({
              disable1: false,
              disable3: false,
            })
            gameModel.gameLottery({
              "merchantId": merchantId,
              // merchantId: '40ed3fb4-6b8a-4606-9cb3-a1e8bba5584b',
              memberId: that.data.memberId,
              refActivityId: that.data.detailData.activityId
            }).then(data => {
              that.setData({
                paursed: true,
                hammer2: true,
              })
              setTimeout(function () {
                that.setData({
                  suspend2: true,
                  gift: data
                })
                setTimeout(function () {
                  that.setData({
                    isShow: false
                  })
                  that.revert()
                }, 1000)
              }, 600)
            }).catch(err => {
              that.setData({
                isShow2: false,
                innerText: `${err.error ? err.error : '暂无此类活动'}`
              })
              that.revert()
            })
          } else {
            that.setData({
              isShow2: false,
              innerText: `暂无此类活动`
            })
            that.revert()
          }
          // _this.getGameLottery(data.memberId)
        }).catch(err => {
          that.setData({
            isShow2: false,
            innerText: `${err.error ? err.error : '暂无此类活动'}`
          })
        })
      } else {
        this.setData({
          authShow: true
        })
      }
    }
  },
  openEgg3: function (e) {
    let that = this;
    if (that.data.disable3) {
      if (app.globalData.isLogin) {
        const merchantId = wx.getStorageSync('merchantId')
        memberModel.userInfo({
          merchantId: merchantId
        }).then(res => {
          that.setData({
            memberId: res.memberId
          })
          if (that.data.detailData.activityId) {
            that.setData({
              disable1: false,
              disable2: false,
            })
            gameModel.gameLottery({
              merchantId: merchantId,
              // merchantId: '40ed3fb4-6b8a-4606-9cb3-a1e8bba5584b',
              memberId: that.data.memberId,
              refActivityId: that.data.detailData.activityId || ''
            }).then(data => {
              that.setData({
                paursed: true,
                hammer3: true,

              })
              setTimeout(function () {
                that.setData({
                  suspend3: true,
                  gift: data
                })
                setTimeout(function () {
                  that.setData({
                    isShow: false
                  })
                  that.revert()
                }, 1000)
              }, 600)
            }).catch(err => {
              that.setData({
                isShow2: false,
                innerText: `${err.error ? err.error : '暂无此类活动'}`
              })
              that.revert()
            })
          } else {
            that.setData({
              isShow2: false,
              innerText: `暂无此类活动`
            })
            that.revert()
          }
          // _this.getGameLottery(data.memberId)
        }).catch(err => {
          that.setData({
            isShow2: false,
            innerText: `${err.error ? err.error : '暂无此类活动'}`
          })
        })
      } else {
        this.setData({
          authShow: true
        })
      }
    }
  },
  revert: function () {
    let that = this;
    that.setData({
      paursed: false,
      hammer1: false,
      suspend1: false,
      hammer2: false,
      suspend2: false,
      hammer3: false,
      suspend3: false,
      disable1: true,
      disable2: true,
      disable3: true
    })
  },
  // 在玩一次
  tryAngain: function () {
    this.setData({
      isShow: true
    })
  },
  // 显示遮罩层
  showModal: function () {
    // 如果已经登录但是没有会员id
    if (app.globalData.isLogin) {
      if (this.data.memberId && this.data.memberId != '') {
        wx.navigateTo({
          url: `/pages/games/winningRecord/index?refActivityId=${this.data.activityType}&memberId=${this.data.memberId}`,
        })
      } else {
        memberModel.userInfo({
          merchantId: wx.getStorageSync('merchantId')
        }).then(data => {
          this.setData({
            memberId: data.memberId
          })
          wx.navigateTo({
            url: `/pages/games/winningRecord/index?refActivityId=${this.data.activityType}&memberId=${this.data.memberId}`,
          })
        })
      }
    } else {
      this.setData({
        authShow: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityType: options.activityType,
      gameConfigName: options.gameConfigName,
      activityId: options.id
    })
    // if (app.globalData.isLogin) {
    if (wx.getStorageSync('merchantId')) {
      this.getGangeDetai()
    } else {
      app.getInfo().then(data => {
        this.getGangeDetai()
      })
    }
    if (app.globalData.isLogin) {
      this.getUserInfo()
    }
    // } else {
    //   this.setData({
    //     authShow: true
    //   })
    // }
  },
  // 授权成功的回调
  loginSucceed: function () {
    // this.getGangeDetai()
    // this.setData({
    //   radarImg: null
    // })
    this.getUserInfo()
  },
  // 获取游戏详情
  getGangeDetai: function () {
    const merchantId = wx.getStorageSync('merchantId')
    // '40ed3fb4-6b8a-4606-9cb3-a1e8bba5584b' ||
    gameModel.gameDetail({
      merchantId: merchantId,
      // activityType: 3,
      // activityStatus: 1,
      // merchantId:'40ed3fb4-6b8a-4606-9cb3-a1e8bba5584b',
      activityType: this.data.activityType,
      activityId: this.data.activityId
      // activityName: this.data.gameConfigName
      // activityName: '幸运砸金蛋啊'
    }).then(data => {
      //  if(data){
      //  data.activityRule = (data.activityRule || '').split('\n')
      if (data === null) {
        wx.showToast({
          title: `${'暂无此类型活动'}`,
          icon: 'none',
          duration: 2000
        })
        return null;
      }
      if (data.activityStatus == 0) {
        wx.showToast({
          title: `${'活动未开始'}`,
          icon: 'none',
          duration: 2000
        })
      } else if (data.activityStatus == 2) {
        wx.showToast({
          title: `${'活动已结束'}`,
          icon: 'none',
          duration: 2000
        })
      }
      this.setData({
        detailData: data,
        gameConfigId: data.activityId
      })
      //  }
    }).catch(err => {
      this.setData({
        isShow2: false,
        innerText: `${err.error ? err.error : '暂无此类活动'}`
      })
    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    const _this = this;
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      _this.setData({
        memberId: data.memberId
      })
      // _this.getGameLottery(data.memberId)
    }).catch(err => {
      _this.setData({
        isShow2: false,
        innerText: `${err.error ? err.error : '暂无此类活动'}`
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.detailData && this.data.detailData.shareTitle,
      // imageUrl: this.data.detailData && this.data.detailData.shareImage,
      imageUrl: this.data.themeUrL,
      path: `pages/games/goldenEggs/index?activityType=${this.data.activityType}&gameConfigName=${this.data.gameConfigName}&shareMemberId=${this.data.memberId}&id=${this.data.activityId}&isShare=1`
    }
  }
})